package org.cuit.lmq.controller;

import cn.hutool.http.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.cuit.lmq.constants.enums.RedisKeyEnum;
import org.cuit.lmq.domain.common.Result;
import org.cuit.lmq.service.UserService;
import org.cuit.lmq.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.UUID;

/**
 * @author Seon
 * @date 2023/3/5
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class SecurityController {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private UserService userService;

    @PostMapping("/modify/email")
    public Result<?> modifyEmail(@RequestBody Map<String, String> parameters) {
        String secretCode = parameters.get("secretCode");
        String email = parameters.get("email");
        String oldEmail = parameters.get("oldEmail");
        if (StringUtils.isBlank(secretCode) || StringUtils.isBlank(email) || StringUtils.isBlank(oldEmail)) {
            return Result.fail("参数错误", HttpStatus.HTTP_BAD_REQUEST);
        }
        String redisKey = RedisKeyEnum.SECRET_CODE.getValue() + oldEmail;
        String validateCode = redisCache.getCacheObject(redisKey);
        if (secretCode.equals(validateCode)) {
            boolean result = userService.modifyEmail(email);
            if (result) {
                redisCache.deleteObject(redisKey);
                return Result.success(true);
            } else {
                return Result.fail("更改失败，请重试", 0);
            }
        } else {
            return Result.fail("不要越界请求，谢谢！", 0);
        }
    }


    @PostMapping("/modify/password")
    public Result<?> modifyPwd(@RequestBody Map<String, String> parameters) {
        String newPwd = parameters.get("newPwd");
        String oldPwd = parameters.get("oldPwd");
        if (StringUtils.isBlank(newPwd) || StringUtils.isBlank(oldPwd)) {
            return Result.fail("参数错误", HttpStatus.HTTP_BAD_REQUEST);
        }
        if (StringUtils.equals(oldPwd, newPwd)) {
            return Result.fail("新密码不能和旧密码一致哦", 0);
        }
        boolean result = userService.modifyPassword(oldPwd, newPwd);
        if (result) {
            return Result.success(true);
        } else {
            return Result.fail("更改失败，请重试", 0);
        }
    }

    @PostMapping("/forgot/password")
    public Result<?> forgotPwd(@RequestBody Map<String, String> parameters) {
        String newPwd = parameters.get("newPwd");
        String secretCode = parameters.get("secretCode");
        String email = parameters.get("email");
        if (StringUtils.isBlank(newPwd) || StringUtils.isBlank(secretCode) || StringUtils.isBlank(email)) {
            return Result.fail("参数错误", HttpStatus.HTTP_BAD_REQUEST);
        }
        String redisKey = RedisKeyEnum.SECRET_CODE.getValue() + email;
        String validateCode = redisCache.getCacheObject(redisKey);
        if (secretCode.equals(validateCode)) {
            boolean result = userService.forgotPassword(email, newPwd);
            if (result) {
                redisCache.deleteObject(redisKey);
                return Result.success(true);
            } else {
                return Result.fail("更改失败，请重试", 0);
            }
        } else {
            return Result.fail("不要越界请求，谢谢！", 0);
        }
    }

    @GetMapping("/validate/code")
    public Result<?> validateCode(@RequestParam("email") String email,
                                  @RequestParam("code") String code) {
        String redisKey = RedisKeyEnum.CAPTCHA.getValue() + email;
        String cacheCode = redisCache.getCacheObject(redisKey);

        if (!StringUtils.isBlank(cacheCode) && cacheCode.equals(code)) {
            String secretCode = UUID.randomUUID().toString();
            redisKey = RedisKeyEnum.SECRET_CODE.getValue() + email;
            redisCache.setCacheObject(redisKey, secretCode);
            return Result.success(secretCode);
        } else {
            return Result.fail("验证码错误，请在稍后重新获取", 0);
        }
    }
}
